package com.study.example.simpleclient.panel;

import com.study.example.simpleclient.ClientMain;
import com.sparrow.example_message.base.req.ReqCreatePacket;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CreatePanel extends JPanel implements ActionListener {

    private JTextField playerNameField;
    private JButton createButton;

    public CreatePanel() {
        setLayout(new GridBagLayout());

        JLabel playerNameLabel = new JLabel("游戏玩家名字:");
        add(playerNameLabel, createGBC(0, 0));

        playerNameField = new JTextField(20);
        add(playerNameField, createGBC(1, 0));

        createButton = new JButton("创建角色");
        createButton.addActionListener(this);
        add(createButton, createGBC(1, 1));
    }

    private GridBagConstraints createGBC(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.insets = new Insets(5, 5, 5, 5);
        return gbc;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String playerName = playerNameField.getText();

        ReqCreatePacket req = new ReqCreatePacket();
        req.setName(playerName);
        ClientMain.channel.writeAndFlush(req);
    }

}

